/**
 * Created by Administrator on 2017/9/18 0018.
 */
operateFormatter = function () {
    return [
        '<a class="update" href="javascript:void(0)" >编辑活动</a>' +
        '&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" style="color: red">删除</a>'
    ].join()
};

window.operateEventes = {
    'click .update': function (e, value, row) {
        showModel("updateBusinessActivity-div", function () {
            $("#updateId").val(row.id);
            $("#name").val(row.name);
        })
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/business/deleteActivity'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

/**
 *执行修改保存操作
 * */
bindClick("button[data-event=submit-updateBusinessActivity]", function () {
    var form = $("#updateBusinessActivityFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse("/web/business/updateBusinessActivity"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('updateBusinessActivity-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

/**
 * 添加
 */
bindClick("#addActivity", function () {
    showAddCategory();
});

/**
 * 弹出添加框
 */
var showAddCategory = function () {
    showModel("addBusinessActivity-div", function () {
        $("#names").val('');
        var getCityId = function () {
            $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 2}, function (id) {
                var select = $("#getCity");
                select.empty();
                select.append('<option value="0">-请选择-</option>');
                if (id.res.code == 40000) {
                    var allData = id.res.data;
                    for (var i = 0; i < allData.length; i++) {
                        select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
                    }
                }
            });
        };
        getCityId();


    });
};

/**
 * 确认添加按钮事件
 */
bindClick("button[data-event=submit-addBusinessActivity]", function () {
    var getBusiness = $("#getBusiness").val();
    $("#busids").val(getBusiness);

    var getCity = $("#getCity").val();
    var getJuniorCity = $("#getJuniorCity").val();
    if(getCity==0){
        hint("请选择省份");
        return;
    }
    if(getJuniorCity==0){
        hint("请选择城市");
        return;
    }
    if(getBusiness==0){
        hint("请选择商家");
        return;
    }
    if($("#names").val()==""){
        hint("请添加活动名称");
        return;
    }
    var form = $("#addBusinessActivityFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/business/addBusinessActivity'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功！');
            //关闭model方法
            closeModel('addBusinessActivity-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});
//城市
var queryJuniorCity = function () {
    var cityId = $("#getCity").val();
    if(cityId==0){
        $("#cityId").hide();
        $("#busId").hide();
        hint("请选择省份");
        return;
    }
    if(cityId==0){
        $("#cityId").hide();
    }else {
        $("#cityId").show();
    }
    $.post($().AbsUrlParse("/web/city/queryCityIdInfo"), {cityId: cityId}, function (id) {
        var select = $("#getJuniorCity");
        select.empty();
        select.append('<option value="0">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};
//商家
var queryBusinessCity = function () {
    var cityId = $("#getJuniorCity").val();
    if(cityId==0){
        $("#busId").hide();
        hint("请选择城市");
        return;
    }
    if(cityId==0){
        $("#busId").hide();
    }else {
        $("#busId").show();
    }
    $.post($().AbsUrlParse("/web/business/queryBusinessCity"), {cityId: cityId}, function (id) {
        var select = $("#getBusiness");
        select.empty();
        select.append('<option value="0">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};

var queryActivity = function () {
    var name = $("#busName").val();
    var url = $().AbsUrlParse("/web/business/queryBusinessActivity?businessName=" + name );
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
};